<?php

namespace app\proxy\model\proxy;

use app\common\model\proxy\User as ProxyUser;
use think\facade\Cache;

class User extends ProxyUser
{

    /**
     * 生成用户认证的token
     */
    private function token($proxy_id)
    {
        $app_id = self::$app_id;
        // 生成一个不会重复的随机字符串
        $guid = \getGuidV4();
        // 当前时间戳 (精确到毫秒)
        $timeStamp = microtime(true);
        // 自定义一个盐
        $salt = 'token_salt';
        return md5("{$app_id}_{$timeStamp}_{$proxy_id}_{$guid}_{$salt}");
    }

    public function checkLogin($user)
    {
        $where['user_name'] = $user['username'];
        $where['password'] = $user['password'];
        $where['is_delete'] = 0;

        if (!$user = $this->where($where)->with(['app'])->find()) {
            $this->error = '用户名或密码不正确111'.$user['password'];
            return false;
        }
        if ($user['is_delete']) {
            $this->error = '登录失败, 当前代理已删除';
            return false;
        }
        if ($user['status'] != 1) {
            $this->error = '登录失败, 当前代理已禁止';
            return false;
        }
        // 写入登录日志
//        LoginLogModel::add($user, \request()->ip(), '登录成功');
        self::$app_id = $user["app_id"];
        $token = $this->token($user['proxy']);
        $this->loginState($token, $user);

        return $token;
    }

    public function loginState($token, $user)
    {
        $app = $user['app'];
        // 保存登录状态
        $state = array(
            'user' => [
                'proxy_id' => $user['proxy_id'],
                'name' => $user['name'],
                'real_name' => $user['real_name'],
                'user_name' => $user['user_name'],
                'mobile' => $user['mobile'],
                'app_id' => $user['app_id'],
            ],
            'app' => $app->toArray(),
            'is_login' => true,
        );
        Cache::tag('cache')->set($token, $state, 86400 * 7);
    }

    /**
     * 修改
     */
    public function edit($data){
        $isexist = $this->where('name','=',$data['name'])->where('proxy_id','<>',$data['proxy_id'])->find();
        if($isexist){
            $this->error = '区域代理名称已存在';
            return false;
        }
        $isexist = $this->where('mobile','=',$data['mobile'])->where('proxy_id','<>',$data['proxy_id'])->find();
        if($isexist){
            $this->error = '手机号码已存在';
            return false;
        }
        return $this->save([
            'name' => $data['name'],
            'real_name' => $data['real_name'],
            'mobile' => $data['mobile'],
        ]);
    }

    /*
    * 修改密码
    */
    public function editPass($data, $user)
    {
        $user_info = User::detail($user['proxy_id']);
        if ($data['password'] != $data['confirmPass']) {
            $this->error = '密码错误';
            return false;
        }
        if ($user_info['password'] != salt_hash($data['oldpass'])) {
            $this->error = '两次密码不相同';
            return false;
        }
        $date['password'] = salt_hash($data['password']);
        $user_info->save($date);
        return true;
    }
}